businessCardTable = null;

(function($)
{
	$(document).ready(function()
	{
		businessCardTable = $('#businessCardTable').dataTable(
		{
			bAutoWidth : false,
			bRetrieve : true,
			bProcessing : false,
			bServerSide : true,
			bFilter : false,
			bInfo : true,
			bLengthChange : false,
			bSort : true,
			sAjaxDataProp: 'businessCards',
			sServerMethod: 'POST',
			sPaginationType: 'full_numbers',
			sAjaxSource : '{0}/businesscard/list'.format(contextPath),
			fnServerParams : function(aoData)
			{
				aoData.push(
				{
					name : "name",
					value : $('input[name="searchName"]').val(),
				},
				{
					name : "phoneNumber",
					value : $('input[name="searchPhoneNumber"]').val(),
				},
				{
					name : "company",
					value : $('select[name="searchCompany"]').val(),
				},
				{
					name : "position",
					value : $('select[name="searchPosition"]').val(),
				},
				{
					name : "description",
					value : $('select[name="searchDescription"]').val(),
				});
			},
			aoColumns :
			[
				{
					mDataProp : 'cardId',
					bSortable : false,
					bVisible : false
				},
				{
					sTitle : '고객명',
					mDataProp : 'name',
					bSortable : true,
					asSorting : [ 'asc', 'desc' ],
					sClass : 'center',
					sWidth : '162px'
				},
				{
					sTitle : '연락처',
					mDataProp : 'phoneNumber',
					bSortable : true,
					asSorting : [ 'asc', 'desc' ],
					sClass : 'nosort',
					sDefaultContent: '-',
					sWidth : '183px'
				},
				{
					sTitle : '회사',
					mDataProp : 'company',
					bSortable : true,
					asSorting : [ 'asc', 'desc' ],
					sDefaultContent: '-',
					sWidth : '159px'
				},
				{
					sTitle : '직책',
					mDataProp : 'position',
					bSortable : true,
					asSorting : [ 'asc', 'desc' ],
					sDefaultContent: '-',
					sWidth : '140px'
				},
				{
					sTitle : '내용',
					mDataProp : 'description',
					bSortable : false,
					sDefaultContent: '-',
					sWidth : '260px'
				}
			],
			fnInfoCallback : function(oSettings, iStart, iEnd, iMax, iTotal, sPre)
			{
				return "전체 " + iMax + "건 중 " + iStart + "부터 " + (iMax < iEnd ? iMax : iEnd) + "까지";
			},
			fnDrawCallback : function(oSettings)
			{
				businessCardTable.$('td').editable('{0}/businesscard/update'.format(contextPath),
				{
					name : 'propertyValue',
					loadtype : 'post',
					callback : function(result, settings)
					{
						var position = businessCardTable.fnGetPosition(this);
						
						businessCardTable.fnUpdate(result.updatedValue, position[0], position[1]);
					},
					submitdata : function(value, settings)
					{
						var position = businessCardTable.fnGetPosition(this);
						
						var rowIndex = position[0];
						var cellIndex = position[2];
						
						var card = businessCardTable.fnGetData(rowIndex);
						
						var propertyName = businessCardTable.fnSettings().aoColumns[cellIndex].mDataProp;
						
						return {
							cardId : card.cardId,
							propertyName : propertyName
						};
					}
				});
			}
		});
		
		$('#searchButton').unbind('click').click(function(event){ businessCardTable.fnDraw(); });
		
		$('#registerButton').unbind('click').click(saveBusinessCard);
	});
	
	/**
	 * 명함 편집 폼을 초기 상태로 되돌린다. 
	 */
	function resetEditForm(event)
	{
		$("#businessCardEditForm").each(function()
		{
			this.reset();
		});
	}
	
	/**
	 * 명함 정보 저장을 요청한다.
	 */
	function saveBusinessCard(event)
	{
		$('#registerButton').attr('disabled', true);
		$('#registerButton').unbind('click');
		
		$.ajax(
		{
			url : '{0}/businesscard/save'.format(contextPath),
			type : 'post',
			cache : false,
			data : $('#businessCardEditForm').serialize(),
			dataType : 'json',
			success : saveBusinessCardCallback
		});
	}
	
	/**
	 * 명함 정보 저장에 대한 콜백을 실행한다.
	 */
	function saveBusinessCardCallback(data)
	{
		$('#registerButton').attr('disabled', false);
		$('#registerButton').click(saveBusinessCard);
		
		resetEditForm();
		
		businessCardTable.fnDraw();
	}
}(jQuery));